package com.bqtv.sport.commen.widget

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import me.jessyan.autosize.utils.AutoSizeUtils


class GridItemDecoration(
    context: Context?, //行数
    private val spanC: Int, //行数
    private val drawMode: LineDrawMode, lineWidthDp: Int, lineHeightDp: Int
) :
    RecyclerView.ItemDecoration() {
    private val mPaint: Paint
    private val lineWidth //px 分割线宽
            : Int
    private val lineHeight //px 分割线宽
            : Int

    /**
     * 分隔线绘制模式,水平，垂直，两者都绘制
     */
    enum class LineDrawMode {
        HORIZONTAL,
        VERTICAL
    }

    init {
        lineWidth = AutoSizeUtils.dp2px(context, lineWidthDp.toFloat())
        lineHeight = AutoSizeUtils.dp2px(context, lineHeightDp.toFloat())
        mPaint = Paint(Paint.ANTI_ALIAS_FLAG)
        mPaint.color = Color.parseColor("#00000000")
        mPaint.style = Paint.Style.FILL
    }

    override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        //left, top, right, bottom
        val childCount1 = parent.childCount
        //        int childCount2 = parent.getLayoutManager().getChildCount();
        //        int childCount3 = parent.getAdapter().getItemCount();
        //        Log.e("count", "getChildCount()=" + childCount1 + "-----getLayoutManager().getChildCount()=" + childCount2 + "----getAdapter().getItemCount()=" + childCount3);
        for (i in 0 until childCount1) {
            val child = parent.getChildAt(i)
            val itemPosition = (child.layoutParams as RecyclerView.LayoutParams).viewLayoutPosition
            val sideOffsetBooleans = getItemSidesIsHaveOffsets(itemPosition)
            if (sideOffsetBooleans[0]) {
                drawChildLeftVertical(child, c, parent)
            }
            if (sideOffsetBooleans[1]) {
                drawChildTopHorizontal(child, c, parent)
            }
            if (sideOffsetBooleans[2]) {
                drawChildRightVertical(child, c, parent)
            }
            if (sideOffsetBooleans[3]) {
                drawChildBottomHorizontal(child, c, parent)
            }
        }
    }

    private fun drawChildBottomHorizontal(child: View, c: Canvas, parent: RecyclerView) {
        val params = child
            .layoutParams as RecyclerView.LayoutParams
        val left = child.left - params.leftMargin - lineWidth
        val right = child.right + params.rightMargin + lineWidth
        val top = child.bottom + params.bottomMargin
        val bottom = top + lineHeight
        c.drawRect(left.toFloat(), top.toFloat(), right.toFloat(), bottom.toFloat(), mPaint)
    }

    private fun drawChildTopHorizontal(child: View, c: Canvas, parent: RecyclerView) {
        val params = child
            .layoutParams as RecyclerView.LayoutParams
        val left = child.left - params.leftMargin - lineWidth
        val right = child.right + params.rightMargin + lineWidth
        val bottom = child.top - params.topMargin
        val top = bottom - lineHeight
        c.drawRect(left.toFloat(), top.toFloat(), right.toFloat(), bottom.toFloat(), mPaint)
    }

    private fun drawChildLeftVertical(child: View, c: Canvas, parent: RecyclerView) {
        val params = child
            .layoutParams as RecyclerView.LayoutParams
        val top = child.top - params.topMargin - lineHeight
        val bottom = child.bottom + params.bottomMargin + lineHeight
        val right = child.left - params.leftMargin
        val left = right - lineWidth
        c.drawRect(left.toFloat(), top.toFloat(), right.toFloat(), bottom.toFloat(), mPaint)
    }

    private fun drawChildRightVertical(child: View, c: Canvas, parent: RecyclerView) {
        val params = child
            .layoutParams as RecyclerView.LayoutParams
        val top = child.top - params.topMargin - lineHeight
        val bottom = child.bottom + params.bottomMargin + lineHeight
        val left = child.right + params.rightMargin
        val right = left + lineWidth
        c.drawRect(left.toFloat(), top.toFloat(), right.toFloat(), bottom.toFloat(), mPaint)
    }

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        //outRect 看源码可知这里只是把Rect类型的outRect作为一个封装了left,right,top,bottom的数据结构,
        //作为传递left,right,top,bottom的偏移值来用的
        val itemPosition = (view.layoutParams as RecyclerView.LayoutParams).viewLayoutPosition
        val sideOffsetBooleans = getItemSidesIsHaveOffsets(itemPosition)

        //如果是设置左边或者右边的边距，就只设置成指定宽度的一半，
        // 因为这个项目中的 Grid 是一行二列，如果不除以二的话，那么中间的间距就会很宽，
        //可根据实际项目需要修改成合适的值
        val left = if (sideOffsetBooleans[0]) lineWidth / 2 else 0
        val top = if (sideOffsetBooleans[1]) lineHeight else 0
        val right = if (sideOffsetBooleans[2]) lineWidth / 2 else 0
        val bottom = if (sideOffsetBooleans[3]) lineHeight else 0
        outRect[left, top, right] = bottom
    }

    /**
     * 顺序:left, top, right, bottom
     *
     * @return boolean[4]
     */
    private fun getItemSidesIsHaveOffsets(itemPosition: Int): BooleanArray {
        //顺序:left, top, right, bottom
        val booleans = booleanArrayOf(false, false, false, false)
        when (itemPosition % spanC) {
        }
        if (itemPosition % spanC == 0) {
            booleans[2] = true
            booleans[3] = true
        } else if (itemPosition % spanC == spanC - 1) {
            booleans[0] = true
            booleans[3] = true
        } else {
            booleans[0] = true
            booleans[2] = true
            booleans[3] = true
        }
        return booleans
    }
}